/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Receipt.java
 *
 * Created on 27 September 2010, 1:24:36 AM
 */

package CAT200;
import java.util.*;
import java.text.*;
import java.sql.*;
/**
 *
 * @author Wai Hong
 */
public class Receipt extends javax.swing.JFrame {

    public static ResultSet result;
    public static String receiptNUM, receiptDATE, orderDATE, orderPAYMENT, orderNum;
    /** Creates new form Receipt */
    public Receipt() {
        executeStatement();
        initComponents();
        setLocationRelativeTo(null);
        runFirst();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        CLOSE = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        receiptNum = new javax.swing.JLabel();
        receiptDate = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        orderDate = new javax.swing.JLabel();
        orderPayment = new javax.swing.JLabel();
        VIEW = new javax.swing.JButton();
        PREVIOUS = new javax.swing.JButton();
        NEXT = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Receipt", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Receipt no.  :");

        CLOSE.setText("Close");
        CLOSE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CLOSEActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setText("Purchase date  :");

        receiptNum.setText("jLabel3");

        receiptDate.setText("jLabel8");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Payment  :");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Order date  :");

        orderDate.setText("jLabel7");

        orderPayment.setText("jLabel8");

        VIEW.setText("View Product");
        VIEW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VIEWActionPerformed(evt);
            }
        });

        PREVIOUS.setText("Previous");
        PREVIOUS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PREVIOUSActionPerformed(evt);
            }
        });

        NEXT.setText("Next");
        NEXT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NEXTActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(receiptNum))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(receiptDate)))
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(orderPayment)
                    .addComponent(orderDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(39, 39, 39))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(VIEW)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PREVIOUS)
                .addGap(18, 18, 18)
                .addComponent(NEXT)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(CLOSE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addContainerGap(122, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(receiptNum)
                    .addComponent(jLabel4)
                    .addComponent(orderDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(receiptDate)
                    .addComponent(orderPayment)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(VIEW)
                    .addComponent(CLOSE)
                    .addComponent(PREVIOUS)
                    .addComponent(NEXT))
                .addGap(28, 28, 28))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        

    private void CLOSEActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        this.dispose();
    }                                     

    private void NEXTActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
         try
        {
            if(result.isLast())
                result.first();

            else
                result.next();

            orderNum = result.getString("ORDER_NUMBER");
            receiptNum.setText(result.getString("RECEIPT_NUM"));
            receiptDate.setText(result.getString("RECEIPT_DATE"));
            orderDate.setText(result.getString("ORDER_DATE"));
            orderPayment.setText(result.getString("PAYMENT"));
        }
        catch(Exception e){  }
    }                                    

    private void PREVIOUSActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        try
        {
            if(result.isFirst())
                result.last();
            else
                result.previous();

            orderNum = result.getString("ORDER_NUMBER");
            receiptNum.setText(result.getString("RECEIPT_NUM"));
            receiptDate.setText(result.getString("RECEIPT_DATE"));
            orderDate.setText(result.getString("ORDER_DATE"));
            orderPayment.setText(result.getString("PAYMENT"));
        }
        catch(Exception e){  }
    }                                        

    private void VIEWActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
        new viewReceipt(orderNum).setVisible(true);
    }                                    

    public static void runFirst()
    {
        try
        {
            result.next();

            orderNum = result.getString("ORDER_NUMBER");
            receiptNum.setText(result.getString("RECEIPT_NUM"));
            receiptDate.setText(result.getString("RECEIPT_DATE"));
            orderDate.setText(result.getString("ORDER_DATE"));
            orderPayment.setText(result.getString("PAYMENT"));
            System.out.println(orderNum);
        }
        catch(Exception e){  }
    }

    public static void executeStatement()
    {
        try
        {
            DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/CAT200-DB", "user", "123");

            Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            result = statement.executeQuery("select RECEIPT.RECEIPT_NUM as RECEIPT_NUM, RECEIPT.RECEIPT_DATE as RECEIPT_DATE, RECEIPT.ORDER_NUMBER as ORDER_NUMBER, ORDERS.ORDER_DATE as ORDER_DATE, ORDERS.PAYMENT as PAYMENT from RECEIPT RECEIPT, ORDERS ORDERS where RECEIPT.CUST_USERNAME = '" + First_Menu.UserAdmin + "'");
        }
        catch(Exception e){  }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Receipt().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton CLOSE;
    private javax.swing.JButton NEXT;
    private javax.swing.JButton PREVIOUS;
    private javax.swing.JButton VIEW;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    public static javax.swing.JLabel orderDate;
    public static javax.swing.JLabel orderPayment;
    public static javax.swing.JLabel receiptDate;
    public static javax.swing.JLabel receiptNum;
    // End of variables declaration                   

}
